package com.hdu.guahao.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hdu.guahao.hosp.repository.DepartmentRepository;
import com.hdu.guahao.hosp.service.DepartmentService;
import com.hdu.hospital.model.hosp.Department;
import com.hdu.hospital.vo.hosp.DepartmentQueryVo;
import com.hdu.hospital.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.repository.query.ExtensionAwareQueryMethodEvaluationContextProvider;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> paramMap) {
        String string = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(string,Department.class);
        Department department1= departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());

        if (department1!=null){
            department1.setUpdateTime(new Date());
            department1.setIsDeleted(0);
            departmentRepository.save(department1);
        }else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        //创建对象
        Pageable pageable = PageRequest.of(page-1,limit);
        //创建example对象
        ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        Example<Department> example = Example.of(department,matcher);
        Page<Department> all = departmentRepository.findAll(example, pageable);

        return all;
    }

    @Override
    public void remove(String hoscode, String depcode) {
        Department departmentByHoscodeAndDepcode = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (departmentByHoscodeAndDepcode!=null){
            departmentRepository.deleteById(departmentByHoscodeAndDepcode.getId());
        }else {
            return;
        }
    }

    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        List<DepartmentVo> departmentVos = new ArrayList<>();
        //根据医院编号查询所有科室
        Department departmentQueryVo = new Department();
        departmentQueryVo.setHoscode(hoscode);
        Example example = Example.of(departmentQueryVo);
        List<Department> departmentList = departmentRepository.findAll(example);

        //根据大科室bigcode分组，在获取大科室下的小可是
        Map<String, List<Department>> map = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));

        System.out.println(map);
        //遍历map集合
        for(Map.Entry<String,List<Department>> entry : map.entrySet() ){
            //大科室编号
            String bigcode  = entry.getKey();
            //大科室编号对应的全部数据
            List<Department> value = entry.getValue();

            //封装大科室
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(bigcode);
            departmentVo.setDepname(value.get(0).getDepname());
            //封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for(Department department: value) {
                DepartmentVo departmentVo2 =  new DepartmentVo();
                departmentVo2.setDepcode(department.getDepcode());
                departmentVo2.setDepname(department.getDepname());
                //封装到list集合
                children.add(departmentVo2);
            }

            //把小科室集合放入大科室
            departmentVo.setChildren(children);
            //放到最后resulit
            departmentVos.add(departmentVo);
        }
        return departmentVos;
    }

    @Override
    public Object getDepName(String hoscode, String depcode) {
        Department departmentByHoscodeAndDepcode = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        return departmentByHoscodeAndDepcode.getDepname();
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
       return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);

    }
}
